<?php
/**
 * CK框架 图形类。用于图形缩略图 水印等功能
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class image
{
	/**
	 * 图像资源
	 * @var imageResource
	 */
	private $_imageResource;
	/**
	 * 生成的文件
	 * @var string
	 */
	private $_makeFile;
	/**
	 * 是否输出图片
	 * @var blooean
	 */
	private $_isMakeImage = true;
	/**
	 * 构造函数
	 * @param string $imageFile 源文件
	 * @param string $makeFile 生成新的图片地址
	 */
	public function __construct($imageFile,$makeFile)
	{
		$this->_makeFile = $makeFile;
		$this->_imageResource = imageResource::createByFile($imageFile);
		$this->_isMakeImage = false;
	}
	/**
	 * 水印
	 * @param string $waterImage 水印图片
	 * @param int $postion 水印位置 
	 * 1 2 3
	 * 4 5 6
	 * 7 8 9这样布局 其他数字为随机点
	 * @param int $pct 透明度
	 */
	public function water($waterImage,$postion = 0,$pct = 80)
	{
		$waterResoure = imageResource::createByFile($waterImage);
		//获取水印图片的宽高
		$waterWidth = $waterResoure->getWidth();
		$waterHeight = $waterResoure->getHeight();
		//获取原始图片的宽高
		$imageWidth = $this->_imageResource->getWidth();
		$imageHeight = $this->_imageResource->getHeight();
		
		$waterWidth > $imageWidth && $waterWidth = $imageWidth;
		$waterHeight >$imageHeight && $waterHeight = $imageHeight;
		
		$waterX = $waterY = 0;
		switch ($postion)
		{
			case 1:
				break;
			case 4:
				$waterX = 0;
				$waterY = ($imageHeight - $waterHeight) / 2;
				break;
			case 7:
				$waterX = 0;
				$waterY = $imageHeight - $waterHeight;
				break;
			case 2:
				$waterX = ($imageWidth - $waterWidth) / 2;
				$waterY = 0;
				break;
			case 5:
				$waterX = ($imageWidth - $waterWidth) / 2;
				$waterY = ($imageHeight - $waterHeight) / 2;
				break;
			case 8:
				$waterX = ($imageWidth - $waterWidth) / 2;
				$waterY = $imageHeight - $waterHeight;
				break;
			case 3:
				$waterX = $imageWidth - $waterWidth;
				$waterY = 0;
				break;
			case 6:
				$waterX = $imageWidth - $waterWidth;
				$waterY = ($imageHeight - $waterHeight) / 2;
				break;
			case 9:
				$waterX = $imageWidth - $waterWidth;
				$waterY = $imageHeight - $waterHeight;
				break;
				default:
				    $waterX = rand(0,($imageWidth - $waterWidth));
					$waterY = rand(0,($imageHeight - $waterHeight));
		}
		// 设定图像的混色模式
		imagealphablending($this->_imageResource->resource, true);
		imagecopymerge($this->_imageResource->resource, $waterResoure->resource, $waterX, $waterY, 0, 0, $waterWidth,$waterHeight,$pct);
	}
	/**
	 * 缩略图
	 * @param int $width 缩略图宽度
	 * @param int $height 缩略图高度
	 */
	public function thumb($width,$height)
	{
		$imageWidth = $this->_imageResource->getWidth();
		$imageHeight = $this->_imageResource->getHeight();
		$ratio = $imageWidth / $imageHeight;  //原图比例
		$thumbRatio = $width / $height; //缩略后比例
		
		$thumbResource = imageResource::createByTrueColor($width, $height, $this->_imageResource->getType());
		if($ratio >= $thumbRatio)
		{
			$imageWidth = $imageHeight * $thumbRatio;
		} else
		{
			$imageHeight = $imageWidth / $thumbRatio;
		}
		imagecopyresampled($thumbResource->resource, $this->_imageResource->resource, 0, 0, 0, 0, $width,$height, $imageWidth, $imageHeight);

		$this->_imageResource = $thumbResource;
	}
	/**
	 * 生成文件
	 */
	public function makeImage()
	{
		$this->outImage($this->_makeFile);
	}
	/**
	 * 浏览器查看图片
	 */
	public function browseImage()
	{
		header("content-type:image/".$this->_imageResource->getType()."\r\n");
		$this->outImage();
	}
	/**
	 * 怕用户忘记使用makeImage&browseImage方法
	 */
	public function __destruct()
	{
		if(!$this->_isMakeImage)
		{
			$this->makeImage();
		}
	}
	/**
	 * 输出图片
	 * @param string $fileName 输出的文件名
	 */
	private function outImage($fileName = '')
	{
		$this->_isMakeImage = true;
		$funcName = $this->_imageResource->getType();
		$funcName = 'image'.$funcName;
		$funcName($this->_imageResource->resource,$fileName);
	}
}


/**
 * image资源类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class imageResource
{
	/**
	 * 资源
	 * @var resource
	 */
	public $resource;
	/**
	 * 宽度
	 * @var int
	 */
	private $_width = 0;
	/**
	 * 高度
	 * @var int
	 */
	private $_height = 0;
	/**
	 * 图像类型
	 * @var string
	 */
	private $_type = '';
	
	private function __construct($resource,$width,$height,$type)
	{
		$this->resource = $resource;
		$this->_width = $width;
		$this->_height = $height;
		$this->_type = $type;
	}
	/**
	 * 创建真彩色资源
	 * @param int $width 宽
	 * @param int $height 高
	 * @param string $type 图形类型
	 * @return imageResource
	 */
	public static function createByTrueColor($width,$height,$type)
	{
		!in_array($type, array('jpeg','png','gif','bmp')) ? $type = 'jpeg' : '';
		return new self(imagecreatetruecolor($width,$height), $width, $height, $type);
	}
	/**
	 * 根据图形文件创建资源
	 * @param string $fileName 文件名
	 * @throws imageNotTypeException
	 * @return @return imageResource
	 */
	public static function createByFile($fileName)
	{
		$imageType = strtolower(substr(strrchr($fileName,"."),1));
		$funcName = '';
		switch ($imageType)
		{
		    case 'jpeg':case 'jpg':
		        $imageType = 'jpeg';
		        break;
		    case 'gif':
		    case 'png':
		    case 'bmp':
		    break;
		    default:
		        throw new imageNotTypeException($imageType);
		}
		$funcName = 'imagecreatefrom'.$imageType;
		$resource = $funcName($fileName);
		return new self($resource, imagesx($resource), imagesy($resource), $imageType);
	}
	/**
	 * 获取宽度
	 */
	public function getWidth()
	{
		return $this->_width;
	}
	/**
	 * 获取高度
	 */
	public function getHeight()
	{
		return $this->_height;
	}
	/**
	 * 获取类型
	 */
	public function getType()
	{
		return $this->_type;
	}
	public function __destruct()
	{
		if(is_resource($this->resource))
		{
			imagedestroy($this->resource);
		}
	}
}

/**
 * 不支持的图片类型异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class imageNotTypeException extends Exception
{
	public function __construct($imageType)
	{
		parent::__construct('image类不支持'.$imageType.'类型图片');
	}
}